package com.lori.netshop.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lori.netshop.mapper.TbContentCategoryMapper;
import com.lori.netshop.pojo.TbContentCategory;
import com.lori.netshop.pojo.TbContentCategoryExample;
import com.lori.netshop.pojo.TbContentCategoryExample.Criteria;
import com.lori.netshop.service.ContentCatService;
import com.lori.netshop.util.NetshopResult;
import com.lori.netshop.util.UITreeNode;
@Service
public class ContentCatServiceimp implements ContentCatService {
	@Autowired
	private TbContentCategoryMapper tbContentCategoryMapper;
	/**
	 * 功能：通过父级ID获得tb_content_category表的数据
	 */

	@Override
	public String queryByID(long parentid) {
		//获得数据
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentid);
		List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
		//创建节点集合
		List<UITreeNode> nodes = new ArrayList<UITreeNode>();
		//遍历，并将数据转到nodes中
		for (TbContentCategory tbContentCategory : list) {
			//创建节点
			UITreeNode uITreeNode = new UITreeNode();
			uITreeNode.setId(tbContentCategory.getId());
			uITreeNode.setText(tbContentCategory.getName());
			uITreeNode.setState(tbContentCategory.getIsParent()?"closed":"open");
			uITreeNode.setParentId(tbContentCategory.getParentId());
			nodes.add(uITreeNode);
		}
		//将list转为json格式字符串
		ObjectMapper mapper = new ObjectMapper();
		String json = "";
		try {
			json = mapper.writeValueAsString(nodes);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}
	/**
	 * 功能：保存分类数据并将对应的父级节点设置为父节点
	 * 
	 */
	@Override
	public NetshopResult save(long parentId, String name) {
		//创建分类对象并补全信息
		TbContentCategory contentcat = new TbContentCategory();
		contentcat.setParentId(parentId);
		contentcat.setName(name);
		contentcat.setCreated(new Date());
		contentcat.setIsParent(false);
		contentcat.setSortOrder(1);
		contentcat.setStatus(1);
		contentcat.setUpdated(new Date());
		//保存信息
		int insert = tbContentCategoryMapper.insert(contentcat);
		//判断上一个节点是否为父节点，不是就把该节点设置为父节点
		TbContentCategory contentcate = tbContentCategoryMapper.selectByPrimaryKey(parentId);
		if(!contentcate.getIsParent()){
			contentcate.setIsParent(true);
			tbContentCategoryMapper.updateByPrimaryKeySelective(contentcate);
		}
		return NetshopResult.ok(contentcat);
	}
	/**
	 * 功能：根据ID更新分类名
	 * 
	 */
	@Override
	public NetshopResult update(long id, String name) {
		//创建pojo
		TbContentCategory category = new TbContentCategory();
		category.setId(id);
		category.setName(name);
		int isSuccess = tbContentCategoryMapper.updateByPrimaryKeySelective(category);
		if(isSuccess == 1){
			return NetshopResult.ok();
		}else{
			return NetshopResult.build(400, "重命名失败");
		}
	}
	@Override
	public NetshopResult delete(long parentId, long id) {
		try {
			//将节点对象的state设置为2，1(正常),2(删除)
			TbContentCategory category = new TbContentCategory();
			category.setId(id);
			category.setStatus(2);
			tbContentCategoryMapper.updateByPrimaryKeySelective(category);
			//查询父节点id为id的节点并删除
			TbContentCategoryExample example = new TbContentCategoryExample();
			Criteria criteria = example.createCriteria();
			criteria.andParentIdEqualTo(id);
			criteria.andStatusEqualTo(1);
			List<TbContentCategory> catelist = tbContentCategoryMapper.selectByExample(example);
			if(catelist.size()>0){
				TbContentCategory category2 = new TbContentCategory();
				category2.setStatus(2);
				for(TbContentCategory contentcategory:catelist){
					category2.setId(contentcategory.getId());
					tbContentCategoryMapper.updateByPrimaryKeySelective(category2);
				}
			}
			//查询父节点是否有其他子节点
			TbContentCategoryExample example2 = new TbContentCategoryExample();
			Criteria criteria2 = example2.createCriteria();
			criteria2.andParentIdEqualTo(parentId);
			criteria2.andStatusEqualTo(1);
			List<TbContentCategory> catelist2 = tbContentCategoryMapper.selectByExample(example2);
			System.out.println(parentId+"-------------"+catelist2.size());
			if(catelist2.size() == 0){
				TbContentCategory contentcat = new TbContentCategory();
				contentcat.setId(parentId);
				contentcat.setIsParent(false);
				tbContentCategoryMapper.updateByPrimaryKeySelective(contentcat);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return NetshopResult.build(400, "删除失败");
		}
		return NetshopResult.ok();
	}

}



